﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"KBinarySearchTree.h"
#include"KVBinarySearchTree.h"
#include<string>

void testBinarySearchTree1()
{
	BSTree<int> Tree;
	Tree.Insert(5);
	Tree.Insert(8);
	Tree.Insert(2);
	Tree.Insert(0);
	Tree.Insert(11);
	Tree.Insert(56);
	Tree.Insert(3);
	Tree.Insert(6);
	Tree.Insert(75);
	Tree.Insert(34);
	Tree.Insert(110);
	Tree.Inorder();
	cout << Tree.Find(56) << endl;
	cout << Tree.Find(111) << endl;
	BSTree<int> Treecopy = Tree;
}
void testBinarySearchTree2()
{
	BSTree<int> Tree;
	int a[] = {8,3,1,10,1,6,4,7,14,13};
	for(auto e:a)
	{
		Tree.Insert(e);
	}
	Tree.Inorder();
	cout<<"开始删除"<< endl;
	for (auto e : a)
	{
		cout<<"删除"<<e<< endl;
		Tree.Erase(e);
		Tree.Inorder();
	}
}
void testKVBinarySearchTree()
{
	KVBinarySearch::BSTree<string, string> dict;
	dict.Insert("left", "左边");
	dict.Insert("right", "右边");
	dict.Insert("insert", "插入");
	dict.Insert("string", "字符串");
	string str;
	while (cin >> str)
	{
		auto ret = dict.Find(str);
		if (ret)
		{
			cout << "->" << ret->_value << endl;
		}
		else
			cout << "无效单词，请重新输入" << endl;
	}
	//ctrl+Z 和换行  退出
}

void count_word()
{
	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
	KVBinarySearch::BSTree<string, int> countTree;
	for (const auto& str : arr)
	{
		// 先查找⽔果在不在搜索树中
		// 1、不在，说明⽔果第⼀次出现，则插⼊<⽔果, 1>
		// 2、在，则查找到的结点中⽔果对应的次数++
		//BSTreeNode<string, int>* ret = countTree.Find(str);
		auto ret = countTree.Find(str);
		if (ret == NULL)
		{
			countTree.Insert(str, 1);
		}
		else
		{
			//修改value
			ret->_value++;
		}
	}
	countTree.Inorder();
	KVBinarySearch::BSTree<string, int>copyTree = countTree;//显示写析构函数之后浅拷贝就导致程序崩溃，这里提供拷贝构造函数
}



int main()
{
	testBinarySearchTree1();
	testBinarySearchTree2();
	//testKVBinarySearchTree();
	//count_word();
	return 0;
}

